﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FrameWork.Lib
{
    public static class IOExt
    {
        public static string ReadAllText(this System.IO.StreamReader pStreamReader)
        {
            var sb = new StringBuilder();

            while (!pStreamReader.EndOfStream)
            {
                var str = pStreamReader.ReadLine().Trim();
                if (str.Length != 0)
                {
                    sb.AppendLine(str);
                }
            }

            return sb.ToString();
        }

        public static void SaveAllText(this string pValue, string pFileName)
        {
            using (var sw = new System.IO.StreamWriter(pFileName))
            {
                sw.Write(pValue);
                sw.Close();
            }
        }

        public static byte[] NextRead(this System.IO.BinaryReader pBinaryReader, out bool pValid)
        {
            byte[] buffer = new byte[1024];
            pValid = pBinaryReader.Read(buffer, 0, 1024) > 0;
            return buffer;
        }

        public static void MakeWritable(this System.IO.FileInfo pFileInfo)
        {
            if ((pFileInfo.Attributes & System.IO.FileAttributes.ReadOnly) == System.IO.FileAttributes.ReadOnly)
            {
                pFileInfo.Attributes = (System.IO.FileAttributes)(((int)pFileInfo.Attributes) & (~(int)System.IO.FileAttributes.ReadOnly));
            }
        }

        public static void MakeWritable(this System.IO.DirectoryInfo pDirectoryInfo)
        {
            if ((pDirectoryInfo.Attributes & System.IO.FileAttributes.ReadOnly) == System.IO.FileAttributes.ReadOnly)
            {
                pDirectoryInfo.Attributes = (System.IO.FileAttributes)(((int)pDirectoryInfo.Attributes) & (~(int)System.IO.FileAttributes.ReadOnly));
            }
        }

        public static Result CopyDirectoryWithFiles(this System.IO.DirectoryInfo pDirectory, bool pRecursive, string pFilePattern, bool pOverwrite)
        {
            return pDirectory.CopyDirectoryWithFiles(pRecursive, pFilePattern, pOverwrite, "");
        }

        public static Result CopyDirectoryWithFiles(this System.IO.DirectoryInfo pDirectory, bool pRecursive, string pFilePattern, bool pOverwrite, string pLocalRoopDirectory)
        {
            var tResult = new Result();

            var tLocalRoopDirectory = "";

            if (pLocalRoopDirectory.IsEmpty())
            {
                tLocalRoopDirectory = string.Format(@".\SQLS\{0}\", pDirectory.Name);
            }
            else
            {
                tLocalRoopDirectory = string.Format(@"{1}{0}\", pLocalRoopDirectory, pDirectory.Name);
            }

            tResult.AddResult(string.Format("New Directory {0}", tLocalRoopDirectory), ResultSeverityType.Info);

            var tFiles = pDirectory.GetFiles(pFilePattern);

            if (tFiles.Length > 0 && !System.IO.Directory.Exists(tLocalRoopDirectory))
            {
                System.IO.Directory.CreateDirectory(tLocalRoopDirectory);
                tResult.AddResult(string.Format("Created New Directory {0}", tLocalRoopDirectory),  ResultSeverityType.Info);
            }

            foreach (var tFile in tFiles)
            {
                var tNewFileName = string.Format("{0}{1}", tLocalRoopDirectory, tFile.Name);

                if (pOverwrite || !System.IO.File.Exists(tNewFileName))
                {
                    tFile.CopyTo(tNewFileName, pOverwrite);
                    tResult.AddResult(string.Format("New File {0}", tNewFileName), ResultSeverityType.Info);
                }
            }

            if (pRecursive)
            {
                var tSubDirectories = pDirectory.GetDirectories();

                foreach (var tSubDirectory in tSubDirectories)
                {
                    tResult.AddResult(tSubDirectory.CopyDirectoryWithFiles(pRecursive, pFilePattern, pOverwrite, tLocalRoopDirectory));
                }
            }

            return tResult;
        }


        //public static Result CopyDirectoryWithFiles(string pDirectory, bool pRecursive, string pFilePattern, bool pOverwrite)
        //{
        //    return CopyDirectoryWithFiles(pDirectory, pRecursive, pFilePattern, pOverwrite, "");
        //}

        //public static Result CopyDirectoryWithFiles(string pDirectory, bool pRecursive, string pFilePattern, bool pOverwrite, string pLocalRootDirectory)
        //{
        //    var tResult = new Result();

        //    var tDirectory = new System.IO.DirectoryInfo(pDirectory);
        //    var tLocalRootDirectory = "";

        //    if (pLocalRootDirectory.IsEmpty())
        //    {
        //        tLocalRootDirectory = string.Format(@".\SQLS\{0}\", tDirectory.Name);
        //    }
        //    else
        //    {
        //        tLocalRootDirectory = string.Format(@"{1}{0}\", pLocalRootDirectory, tDirectory.Name);
        //    }

        //    tResult.AddResult(string.Format("New Directory {0}", tLocalRootDirectory), ResultSeverityType.Info);

        //    var tFiles = tDirectory.GetFiles(pFilePattern);

        //    if (tFiles.Length > 0 && !System.IO.Directory.Exists(tLocalRootDirectory))
        //    {
        //        System.IO.Directory.CreateDirectory(tLocalRootDirectory);
        //        tResult.AddResult(string.Format("Created New Directory {0}", tLocalRootDirectory), ResultSeverityType.Info);
        //    }

        //    foreach (var tFile in tFiles)
        //    {
        //        var tNewFileName = string.Format("{0}{1}", tLocalRootDirectory, tFile.Name);

        //        if (pOverwrite || !System.IO.File.Exists(tNewFileName))
        //        {
        //            tFile.CopyTo(tNewFileName, pOverwrite);
        //            tResult.AddResult(string.Format("New File {0}", tNewFileName), ResultSeverityType.Info);
        //        }
        //    }

        //    if (pRecursive)
        //    {
        //        var tSubDirectories = tDirectory.GetDirectories();

        //        foreach (var tSubDirectory in tSubDirectories)
        //        {
        //            tResult.AddResult(CopyDirectoryWithFiles(tSubDirectory.FullName, pRecursive, pFilePattern, pOverwrite, tLocalRootDirectory));
        //        }
        //    }

        //    return tResult;
        //}
    }
}
